package com.ztf0337.mycurriculumdesign.service;

import android.content.Context;
import android.widget.LinearLayout;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.dao.ParkingSpaceDao;
import com.ztf0337.mycurriculumdesign.database.AppDatabase;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;

import java.util.ArrayList;
import java.util.List;

public class ParkingSpaceService {

    private final ParkingSpaceDao parkingSpaceDao;

    public ParkingSpaceService(Context context) {
        AppDatabase db = AppDatabase.getInstance(context);
        this.parkingSpaceDao = db.parkingSpaceDao();
    }

    public long insert(ParkingSpace parkingSpace) {
        return parkingSpaceDao.insert(parkingSpace);
    }

    public void delete(ParkingSpace parkingSpace) {
        parkingSpaceDao.delete(parkingSpace);
    }

    public List<ParkingSpace> getParkingSpacesByParkLotId(Long id) {
        return parkingSpaceDao.getParkingSpacesByParkLotId(id);
    }

    public ParkingSpace getParkingSpaceById(Long id) {
        return parkingSpaceDao.getParkingSpaceById(id);
    }

    public void update(ParkingSpace parkingSpace) {
        parkingSpaceDao.update(parkingSpace);
    }

    public List<ParkingSpace> filterData(int status, int type, Long parkLotId) {

        List<ParkingSpace> ps = parkingSpaceDao.getParkingSpacesByParkLotId(parkLotId);
        if (ps.isEmpty()) {
            return ps;
        }

        if (status == 0 && type == 0) {
            return ps;
        }

        String t = null;
        if (type == 1) {
            t = "ELECTRIC_SMALL_CAR";
        } else if (type == 2) {
            t = "ELECTRIC_MEDIUM_CAR";
        } else if (type == 3) {
            t = "ELECTRIC_LARGE_CAR";
        } else if (type == 4) {
            t = "GASOLINE_SMALL_CAR";
        } else if (type == 5) {
            t = "GASOLINE_MEDIUM_CAR";
        } else if (type == 6) {
            t = "GASOLINE_LARGE_CAR";
        }
        Boolean s = null;
        if (status == 1) {
            s = true;
        } else if (status == 2) {
            s = false;
        }

        List<ParkingSpace> r1;
        if (t != null) {
            r1 = new ArrayList<>();
            for (ParkingSpace p : ps) {
                if (p.getType().equals(t)) {
                    r1.add(p);
                }
            }
        } else {
            r1 = new ArrayList<>(ps);
        }

        List<ParkingSpace> r2;
        if (s != null) {
            r2 = new ArrayList<>();
            for (ParkingSpace p : r1) {
                if (p.getStatus().equals(s)) {
                    r2.add(p);
                }
            }
        } else {
            r2 = new ArrayList<>(r1);
        }
        return r2;
    }

    public List<ParkingSpace> searchParkingSpaces(String content,Long parkLotId) {
        return parkingSpaceDao.searchParkingSpaces(content, parkLotId);
    }
}
